//
//  GYShopinfoView.m
//  GYDemo
//
//  Created by Mrzhang on 2018/4/10.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYShopinfoView.h"
#import "GYShopInfoViewModel.h"
#import "Head.h"
@interface GYShopinfoView ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)ShopInfoModel* inforModel;
@property(nonatomic,assign)BOOL isCollect;
@end

@implementation GYShopinfoView

-(ShopInfoModel *)inforModel{
    
    if (!_inforModel) {
        _inforModel=[[ShopInfoModel alloc] init];
    }
    return _inforModel;
}
-(GCBaseTableView *)myTable
{
    if (!_myTable) {
        _myTable=[[GCBaseTableView alloc]initWithFrame:self.bounds style:UITableViewStyleGrouped];
        _myTable.delegate=self;
        _myTable.dataSource=self;
        _myTable.separatorStyle=UITableViewCellSeparatorStyleNone;
        _myTable.backgroundColor=[UIColor whiteColor];
    }
    return _myTable;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.myTable];
    }
    return self;
}

#pragma mark - delegate
#pragma mark - table delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0||section==3) {
        return 1;
    }
    return 4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0)
    {
        static NSString *identify = @"sectioncell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.backgroundColor=Main_Color_Black;
        }

        [cell.contentView removeAllSubviews];
        

        
        UILabel* titleLab=[[UILabel alloc]init];
        
        titleLab.textColor=[UIColor whiteColor];
        titleLab.font=SYSTEMFONT(15);
        titleLab.frame=WFCGRectMake(17.5, 16.5, 296-18, 21);
        titleLab.text=_inforModel.companyname;
        [cell.contentView addSubview:titleLab];
        
        
        UILabel* markLab=[[UILabel alloc]init];
        markLab.textColor=[UIColor whiteColor];
        markLab.font=SYSTEMFONT(12);
        markLab.text=@"信誉";
        markLab.frame=WFCGRectMake(17.5, 42.5, 55, 16.5);
        [cell.contentView addSubview:markLab];
        
        
        NSString* str=self.inforModel.xlevel;
        
        NSMutableArray * arr = [NSMutableArray arrayWithCapacity:0];
        // 遍历字符串，按字符来遍历。每个字符将通过block参数中的substring传出
        [str enumerateSubstringsInRange:NSMakeRange(0, str.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
            [arr addObject:substring];
        }];
        
        for (int i=0; i<[arr count]; i++)
        {
            UIImageView* iv=[[UIImageView alloc] init];
            if ([arr[i] isEqualToString:@"A"])
            {
                iv.image=[UIImage imageNamed:@"xydj_a"];
            }
            if ([arr[i] isEqualToString:@"B"])
            {
                iv.image=[UIImage imageNamed:@"xydj_b"];
            }
            iv.frame=WFCGRectMake(56+20*i, 45, iv.image.size.width, iv.image.size.height);
            [cell.contentView addSubview:iv];
        }
        
        
        
        
        UIImageView* line=[[UIImageView alloc] init];
        line.frame=WFCGRectMake(296, 22.5, 1, 30);
        line.backgroundColor=RGBACOLOR(164, 170, 179, 1);
        [cell.contentView addSubview:line];
        
        UILabel* countLab=[[UILabel alloc]init];
        countLab.textColor=RGBACOLOR(245, 266, 35, 1);
        countLab.font=SYSTEMFONT(15);
        countLab.textAlignment=NSTextAlignmentCenter;
        countLab.frame=WFCGRectMake(296, 19, 375-296, 21);
        countLab.text=[NSString stringWithFormat:@"%ld",_inforModel.salecount];
        [cell.contentView addSubview:countLab];
        
        UILabel* deallab=[[UILabel alloc]init];
        deallab.textAlignment=NSTextAlignmentCenter;
        deallab.textColor=[UIColor whiteColor];
        deallab.font=SYSTEMFONT(12);
        deallab.text=@"已成交";
        deallab.frame=WFCGRectMake(296, 40, 375-296, 16.5);
        [cell.contentView addSubview:deallab];
        
        
        
        
        return cell;
    }
    
    if (indexPath.section==1||indexPath.section==2)
    {
        static NSString *identify = @"section1cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        [cell.contentView removeAllSubviews];
        
        NSArray* titleArr=indexPath.section==1?@[@"联系人",@"手机号",@"QQ",@"地址"]:@[@"采购产品种类",@"主营品种",@"开户行",@"账号"];
        UILabel* nameLab=[[UILabel alloc] init];
        nameLab.frame=WFCGRectMake(12.5, 0, 100, 35);
        nameLab.textColor=Main_Color_Black;
        nameLab.font=SYSTEMFONT(15);
        [cell.contentView addSubview:nameLab];
        nameLab.text=titleArr[indexPath.row];
        
        UILabel* ValueLab=[[UILabel alloc] init];
        ValueLab.frame=WFCGRectMake(120, 0,375-125, 35);
        ValueLab.textColor=Main_Color_Black;
        ValueLab.font=SYSTEMFONT(15);
        [cell.contentView addSubview:ValueLab];
        //ValueLab.text=;
        if (indexPath.section==1)
        {
            ValueLab.text=indexPath.row==0?_inforModel.name:indexPath.row==1?_inforModel.linkmobile:indexPath.row==2?_inforModel.qq:_inforModel.address;
            if (indexPath.row==3)
            {
                CGSize temSize=[WFFunctions WFStrGetSize:_inforModel.address width:WFCGFloatX(375-125) font:SYSTEMFONT(15)];
                if (temSize.height>WFCGFloatY(35)) {
                   ValueLab.frame=WFCGRectMake(120, 10,375-125, WFCGFloatY(temSize.height));
                    ValueLab.height=temSize.height;
                    ValueLab.numberOfLines=0;
                }
            }
        }else
        {
            
            ValueLab.text=indexPath.row==0?_inforModel.buytypename:indexPath.row==1?_inforModel.saletypename:indexPath.row==2?_inforModel.bankname:_inforModel.bankaccount;
            
            if (indexPath.row==0)
            {
                CGSize temSize=[WFFunctions WFStrGetSize:_inforModel.buytypename width:WFCGFloatX(375-125) font:SYSTEMFONT(15)];
                if (temSize.height>WFCGFloatY(35)) {
                    ValueLab.frame=WFCGRectMake(120, 10,375-125, WFCGFloatY(temSize.height));
                    ValueLab.numberOfLines=0;
                }
            }
            if (indexPath.row==1)
            {
                CGSize temSize=[WFFunctions WFStrGetSize:_inforModel.saletypename width:WFCGFloatX(375-125) font:SYSTEMFONT(15)];
                if (temSize.height>WFCGFloatY(35)) {
                    ValueLab.frame=WFCGRectMake(120, 10,375-125, WFCGFloatY(temSize.height));
                    ValueLab.numberOfLines=0;
                    ValueLab.height=temSize.height;
                }
            }
            if (indexPath.row==2||indexPath.row==3)
            {
                ValueLab.frame=WFCGRectMake(120, 0,375-125, 35);
            }
        }

        return cell;
    }
    static NSString *identify = @"section2cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    [cell.contentView removeAllSubviews];

    UILabel* contentLab=[[UILabel alloc]init];
    contentLab.numberOfLines=0;
    contentLab.textColor=Main_Color_Black;
    contentLab.font=SYSTEMFONT(14);
    
    contentLab.text=_inforModel.detail;
    [cell.contentView addSubview:contentLab];
    CGSize temSizeH=[WFFunctions WFStrGetSize:_inforModel.detail width:WFCGFloatX(375-28) font:SYSTEMFONT(14)];
    contentLab.frame=WFCGRectMake(14, 15.5, temSizeH.width, temSizeH.height);
    
    //description
    
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return WFCGFloatY(75);
        
    }
    if (indexPath.section==1)
    {
        if (indexPath.row==3)
        {
            CGSize temSize=[WFFunctions WFStrGetSize:_inforModel.address width:WFCGFloatX(375-125) font:SYSTEMFONT(15)];
            if (temSize.height<WFCGFloatY(35))
            {
                return WFCGFloatY(35);
            }
            return WFCGFloatY(10)+temSize.height;
        }
        return WFCGFloatY(35);
    }
    if (indexPath.section==2)
    {
        if (indexPath.row==0)
        {
            CGSize temSize=[WFFunctions WFStrGetSize:_inforModel.buytypename width:WFCGFloatX(375-125) font:SYSTEMFONT(15)];
            if (temSize.height<WFCGFloatY(35)) {
                
                return WFCGFloatY(35);
            }
            return WFCGFloatY(10)+temSize.height;
        }
        if (indexPath.row==1)
        {
            CGSize temSize=[WFFunctions WFStrGetSize:_inforModel.saletypename width:WFCGFloatX(375-125) font:SYSTEMFONT(15)];
            if (temSize.height<WFCGFloatY(35)) {
                return WFCGFloatY(35);
            }
            return WFCGFloatY(10)+temSize.height;
        }
        
        return WFCGFloatY(35);
    }
     CGSize temSizeH=[WFFunctions WFStrGetSize:_inforModel.detail width:WFCGFloatX(375-28) font:SYSTEMFONT(14)];
    return temSizeH.height+20;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    view.backgroundColor=[UIColor whiteColor];
    if (section!=0) {
        UILabel* markLab=[[UILabel alloc]initWithFrame:WFCGRectMake(12.5,0, 200, 45)];
        markLab.textColor=Main_Color_Black;
        markLab.font=SYSTEMFONT(15);
        markLab.text=section==1?@"联系方式":section==2?@"企业信息":@"企业介绍";
        [view addSubview:markLab];
        
        UIImageView* line=[[UIImageView alloc] init];
        line.frame=WFCGRectMake(0, 45, self.width, 0.5);
        line.backgroundColor=RGBACOLOR(221, 221, 221, 1);
        [view addSubview:line];
    }
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return .0001f;
    }
    return WFCGFloatY(45);
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    view.backgroundColor=RGBACOLOR(247, 247, 247, 1);
    return view;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==3) {
        return .0001f;
    }
    return 7;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

-(void)refresh:(ShopInfoModel *)model
{
    _inforModel=model;
    [_myTable reloadData];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
